package com.hj.controller;


import com.hj.domain.User;
import com.hj.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/my")
//@DefaultProperties(defaultFallback = "defaultFallBack") //开启默认
public class MyController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    @HystrixCommand(defaultFallback = "testFindAllFallBack")
    public String findAll(){
        String url = "http://provider-service/user/findAll";
        return restTemplate.getForObject(url, String.class);
    }

    @RequestMapping("/consumerById/{id}")
    public User consumerById(@PathVariable("id") Integer id){
        return userService.findUserById(id);
    }

    //针对某个方法降级
    public String testFindAllFallBack(){
        return "findAll方法服务降级";
    }


    //默认全局降级方法
    public String defaultFallBack(){
        return "你好，服务器正忙，请稍后重试";
    }
}
